import os
import time


class RoxySearch:

    def __init__(self):
        """
        Roxy 搜索模块
        """

        self.res = []
        self.res_num = 0
        self.sword = ""

    def conn_es(self, sword):
        """
        调用Everything进行搜索
        :param sword:
        :return:
        """

        self.sword = sword
        self.res = []
        response = os.popen("DLL\\es " + self.sword).read()
        r2 = response.split("\n")
        for i in r2:
            self.res.append(i)

    def roxysearch(self, sword, search_file, res_file):
        """
        调用自研引擎搜索文件
        :param sword:
        :param search_file:
        :param res_file:
        :return:
        """
        self.sword = sword
        self.res = []
        search_res = ""
        # with open (res_file, "w", encoding='utf-8') as f:
        #     f.write("")
        with open (search_file, "w") as f:
            f.write(sword)
        with open (res_file, "r") as f:
            read_times = 0
            while True:
                time.sleep(0.5)
                try:
                    search_res = f.read()
                except Exception as e:
                    print(e)
                if search_res != "":
                    break
                read_times += 1
                if read_times >= 10:
                    break
        self.res = search_res.split("\n")

    def return_res(self):
        return self.res


if __name__ == '__main__':
    roxy_search = RoxySearch()
    #roxy_search.conn_es("python")
    roxy_search.roxysearch("hello", "D:/1.txt", "D:/2.txt")
    print(roxy_search.return_res())